<?php
/**
 * Table schema class
 * @author Artur Kmera <artur.kmera@3code.pl>
 * @version 0.9
 * @package ORM
 * @subpackage Schema
 * @link http://arrowplatform.org/orm
 * @copyright 2011 Arrowplatform 
 * @license GNU LGPL
*/

class Table implements ISchemaElement{
	/**
	 * Class path ( slash separated )
	 * @var string
	 */
	public $classPath;
	
	/**
	 * Table class name ( Domain object class )
	 * @var string
	 */
	public $class;
	
	/**
	 * Base class path ( slash separated )
	 * @var string
	 */
	public $baseClassPath;
	
	/**
	 * Base class
	 * @var string
	 */
	public $baseClass;
	
	/**
	 * Db table name
	 * @var string
	 */
	public $table;
	
	/**
	 * Table fields ( array )
	 * @var Field []
	 */
	public $fields = array();
	
	/**
	 * Foreign keys
	 * @var ForeignKey []
	 */
	public $foreignKeys = array();
	
	/**
	 * Indexes
	 * @var Index []
	 */
	public $indexes = array();
	
	
	/**
	 * Return field by given name
	 * @param Field $name
	 */	
	public function getFieldByName($name){
		foreach($this->fields as $field)
			if($field->name == $name)
				return $field;
		throw new SchemaException("Field '{$name}' not exists in table '{$this->table}'");
	}

	/**
	 * @todo implement
	 */
	public function getIndexByName(){}
	

	/**
	 * (non-PHPdoc)
	 * @see ISchemaElement::toString()
	 */
	public function toString(){
		return "Type: Table, Class: {$this->class}, Table: {$this->table}";
	}

	/**
	 * (non-PHPdoc)
	 * @see ISchemaElement::toArray()
	 */
	public function toArray(){
		$fields = array();
		
		foreach($this->fields as $key => $field){
			$fields[$key] = $field->toArray();
		}
		
		return array(
			"class" => $this->class,
			"classPath" => $this->classPath,
			"baseClass" => $this->baseClass,
			"baseClassPath" => $this->baseClassPath,
			"table" => $this->table,
			"fields" => $fields,
			"indexes" => $this->indexes,
			"foreignKeys" => $this->foreignKeys,
			
		);
	}
}
?>